'''
4.公交价格饼图
'''

from pyecharts.charts import Pie
import pyecharts.options as opts
import mysql.connector
from pyecharts.globals import ThemeType

# 创建连接
cnx = mysql.connector.connect(user='root', password='123456',
                              host='localhost', database='bus_view')

# 创建游标对象
cursor = cnx.cursor()

# SQL 查询语句
sql1 = "SELECT COUNT(*) FROM bus_count where bus_price = 1"
cursor.execute(sql1)
results1=cursor.fetchone()

sql2 = "SELECT COUNT(*) FROM bus_count where bus_price = 2"
cursor.execute(sql2)
results2=cursor.fetchone()
cursor.close()

x_data = ["1元车价",  "2元车价"]
y_data = [results1[0], results2[0]]
print("开始绘制价格饼图...")
pie = (
    Pie(opts.InitOpts(theme=ThemeType.DARK,chart_id='pie',page_title='公交价格饼图',bg_color="rgba(16, 12, 42,0.8)"))
    .set_global_opts(title_opts=opts.TitleOpts(title='公交价格占比',
                                               subtitle='The proportion of bus prices',
                                               title_textstyle_opts=opts.TextStyleOpts(
                                                   font_size=10,  # 标题的字体大小
                                                   font_family='微软雅黑',
                                               ),
                                               pos_left='center',
                                               pos_top='15px'))
    .add(
        series_name="公交价格",
        data_pair=[list(z) for z in zip(x_data, y_data)],
        radius=["50%", "70%"],
        label_opts=opts.LabelOpts(is_show=False, position="center"),
    )
    .set_global_opts(legend_opts=opts.LegendOpts(pos_left="legft", orient="vertical"))
    .set_series_opts(
        tooltip_opts=opts.TooltipOpts(
            trigger="item", formatter="{a} <br/>{b}: {c} ({d}%)"
        ),
        # label_opts=opts.LabelOpts(formatter="{b}: {c}")
    )

)
pie.render("4.价格饼图.html")